package com.tanhua.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.tanhua.api.houtai.SoulApi;
import com.tanhua.api.houtai.SoulQuestionApi;
import com.tanhua.domain.mongo.QuestionList;
import com.tanhua.domain.mongo.QuestionOption;
import com.tanhua.domain.mongo.Soul;
import com.tanhua.service.SoulQuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author user
 */
@Service
public class SoulQuestionServiceImpl implements SoulQuestionService {

    @Reference
    public SoulApi soulApi;

    @Reference
    public SoulQuestionApi soulQuestionApi;

    /**
     * 查询题库
     *
     * @param souId
     * @return
     */
    @Override
    public ResponseEntity getSoulQuestionById(Integer souId) {
        Soul soul = soulApi.findSoulId(souId);
        List<QuestionList> lists = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(soul.getQuestionIds())) {
            for (String questionId : soul.getQuestionIds()) {
                QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(Integer.parseInt(questionId));
                lists.add(questionList);
            }
        }
        return ResponseEntity.ok(lists);
    }


    /**
     * 根据id删除题
     * * 试卷id和
     * * 题的id
     *
     * @param soulId     试卷id
     * @param questionId 题的id
     * @return
     */
    @Override
    public ResponseEntity deleteQuestionById(Integer soulId, Integer questionId) {
//        查询出对应的试卷
        Soul soul = soulApi.findSoulId(soulId);
//        移除试卷中对应的题的id
        if (soul != null) {
            for (int i = 0; i < soul.getQuestionIds().size(); i++) {
                if (soul.getQuestionIds().get(i).equals(questionId.toString())) {
                    soul.getQuestionIds().remove(i);
                }
            }
        }
//        更新试卷
        soulApi.updateSoul(soul);
//        删除题
        soulQuestionApi.deleteQuestionById(questionId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改题
     *
     * @return
     */
    @Override
    public ResponseEntity updateQuestion(QuestionList questionList) {
        soulQuestionApi.updateQuestion(questionList);
        return ResponseEntity.ok(null);
    }

    /**
     * 添加题
     *
     * @return
     */
    @Override
    public ResponseEntity addQuestion(Integer soulId, QuestionList questionList) {
//        questionList.setQuestionId(RandomUtil.randomLong(0, 100));
        soulQuestionApi.addQuestion(questionList);
        Soul soul = soulApi.findSoulId(soulId);
        if (!CollectionUtils.isNotEmpty(soul.getQuestionIds())) {
//            没有题库id
            List<String> qessstionids = new ArrayList<>();
            qessstionids.add(questionList.getQuestionId().toString());
            soul.setQuestionIds(qessstionids);
        } else {
            soul.getQuestionIds().add(questionList.getQuestionId().toString());
        }
        soulApi.updateSoul(soul);
        return ResponseEntity.ok(null);
    }


    /**
     * 查询题所对应的选项
     *
     * @param questionId 题的id
     * @return
     */
    @Override
    public ResponseEntity queryOptions(Integer questionId) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(questionId);
        return ResponseEntity.ok(questionList.getOptions());
    }

    /**
     * 删除选项
     * TODO:
     *
     * @param questionId
     * @return
     */
    @Override
    public ResponseEntity deleteOption(Integer questionId, Integer id) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(questionId);
        if (questionList != null) {
            for (int i = 0; i < questionList.getOptions().size(); i++) {
                if (questionList.getOptions().get(i).getId().equals(id.toString())) {
//                    删除选项
                    questionList.getOptions().remove(i);
                }
            }
        }
        //        更新数据
        soulQuestionApi.updateQption(questionList);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改选项
     *
     * @param questionId 题的id
     * @return
     */
    @Override
    public ResponseEntity updateOption(Integer questionId, QuestionOption questionOption) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(questionId);
        if (questionList != null) {
            for (QuestionOption option : questionList.getOptions()) {
                if (option.getId().equals(questionOption.getId())) {
                    BeanUtils.copyProperties(questionOption, option);
                }
            }
        }
        soulQuestionApi.updateQption(questionList);
        return ResponseEntity.ok(null);
    }


    /**
     * 添加选项
     *
     * @param questionId     题的id
     * @param questionOption 题的
     * @return
     */
    @Override
    public ResponseEntity addOption(Integer questionId, QuestionOption questionOption) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(questionId);
        List<QuestionOption> options = questionList.getOptions();
        if (CollectionUtils.isNotEmpty(options)) {
            options.add(questionOption);
        } else {
            List<QuestionOption> options1 = new ArrayList<>();
            options1.add(questionOption);
            questionList.setOptions(options1);
        }
        soulQuestionApi.updateQption(questionList);
        return ResponseEntity.ok(null);
    }

    /**
     * 根据id查询题库
     *
     * @param questionId
     * @return
     */
    @Override
    public ResponseEntity getQuestionById(String questionId) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(Integer.parseInt(questionId));
        return ResponseEntity.ok(questionList);
    }

    /**
     * @param questionId
     * @param option
     * @return
     */
    @Override
    public ResponseEntity getQuestionOptionById(String option, String questionId) {
        QuestionList questionList = soulQuestionApi.fingSoulQuestionbyId(Integer.parseInt(questionId));
        if (CollectionUtils.isNotEmpty(questionList.getOptions())) {
            for (int i = 0; i < questionList.getOptions().size(); i++) {
                if (questionList.getOptions().get(i).getOption().equals(option)) {
                    return ResponseEntity.ok(questionList.getOptions().get(i));
                }
            }
        }
        return ResponseEntity.ok(null);
    }


}
